package com.sun.threadpool;

import java.util.concurrent.*;

public class ThreadPoolMain {
    public static void main(String[] args) throws InterruptedException {
        ThreadPoolExecutor threadPoolExecutor = getThreadPoolExecutor();
        threadPoolExecutor.execute(ThreadPoolMain::task);
        Thread.sleep(500);
        Future<?> submit = threadPoolExecutor.submit(ThreadPoolMain::task);
        try {
            submit.get();
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }
    }

    public static void task(){
        System.out.print("线程【"+ Thread.currentThread().getName() + "】正在执行任务...");
        throw new RuntimeException("任务执行异常");
    }

    private static ThreadPoolExecutor getThreadPoolExecutor() {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                5,
                10,
                1000,
                TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<Runnable>(10),
                new ThreadPoolExecutor.DiscardOldestPolicy()
        );
        return threadPoolExecutor;
    }
}
